<div class="wmm-module">
    <div class="layui-form-item">
        <form id="search-form" class="layui-form" action="javascript:;">
            <div class="layui-inline">
                <input type="text" name="groupName" autocomplete="off" lay-verify="required" placeholder="分类名称"
                    class="layui-input">
            </div>
            <div class="layui-inline">
                <button type="submit"  class="layui-btn layui-btn-normal layui-btn-sm datatable-search"><i class="layui-icon layui-icon-search search-icon"></i>
                    查询</button>
            </div>
            <div class="layui-inline">
            <button class="layui-btn layui-btn-normal layui-btn-sm" onclick="renders.box(null,true)">添加</button>
            </div>
        </form>
    </div>

    <div>
        <div id="tb-sys-tmp-group" lay-filter="tb-sys-tmp-group">
        </div>
    </div>
</div>


<template id="tmp-group-form">
    <div style="padding:10px">
        <form class="layui-form" lay-filter="group-form" action="javascript:;">
            <input type="hidden" value="" name="id" />

            <div class="layui-form-item">
                <label class="layui-form-label">名称<span class="required-star">*</span></label>
                <div class="layui-input-block">
                    <input type="text" name="groupName" placeholder="分类名称" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">描述<span class="required-star">*</span></label>
                <div class="layui-input-block">
                    <input type="text" name="groupDescription" lay-verify="required" placeholder="分类描述" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn lay-submit layui-btn-normal" lay-submit lay-filter="resource">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</template>



<script>

    new Wmm({
        vars: {
            table: {},
            appList: undefined,
            reqUrl: "/sysbeetletemplategroup/"
        },

        imports: ["form","api","datatable","req","sysTools"],
        listener: function () {

            fb.form.on('submit(resource)', function (data) {
                var dataObject = data.field;
                console.log("========dataObject=============:"+JSON.stringify(dataObject))
                if (dataObject.id) {
                    //update
                    req.put(vars.reqUrl,dataObject,function(data){
                        console.log("======进入成功======");
                        console.log("data:"+JSON.stringify(data));
                        if(data.code == 0){
                            layer.closeAll();
                            layer.msg("修改成功",{icon:6});
                            vars.table.flush();
                        }
                    })
                    return;
                }
                //insert
                console.log("=========dataObject========="+dataObject);
                req.post(vars.reqUrl,dataObject,function(data){
                    console.log("======进入成功======");
                    console.log("data:"+JSON.stringify(data));
                    if(data.code == 0){
                        layer.closeAll();
                        layer.msg("添加成功",{icon:6});
                        vars.table.flush();
                    }
                })
            });
        },
        requests: {

        },
        renders: {
            box: function (data, isCreate) {
                layer.open({
                    type: 1,
                    title: (isCreate ? "添加" : "修改") + "模板分类",
                    content: document.querySelector("#tmp-group-form").innerHTML,
                    area: ['500px', '330px'],
                    success: function () {
                        if (!isCreate) {
                            //表单初始赋值
                            fb.form.val('group-form', data)
                        }
                        fb.form.render();
                    }
                });
            },
            del_box: function(){
                var tables = vars.table.getCheckedData();
                var flag = false;
                if(tables.length == 0){
                    flag = true;
                    layer.msg("请选择复选框？",{icon:5});
                }
                if(flag){
                    return;
                }

                layer.confirm("是否一键删除复选框选中的数据?",function(index){
                    var tableIds = '';
                    console.log(tables);
                    for(var i =0;i<tables.length;i++){
                        tableIds += tables[i].id +",";
                    }
                    tableIds = tableIds.substr(0,tableIds.length-1);
                    console.log("=========:"+tableIds);
                    req.get(vars.reqUrl+"batchDelete?tablesId="+tableIds,function(data){
                        console.log("======进入成功======");
                        console.log("data:"+JSON.stringify(data));
                        if(data.code == 0){
                            layer.msg("一键删除成功",{icon:6});
                            vars.table.flush();
                        }
                    })

                    layer.close(index);
                })

            },
            render: function () {
                vars.table = new DataTable({
                    el: "tb-sys-tmp-group",
                    formEl: "search-form",
                    url: vars.reqUrl,
                    multiple:true,
                    firstQueryData: function () {  //
                        var params = fb.router.state;
                        fb.form.val("search-form", params);
                        if (params.appsId) {
                            if (typeof params.appsId === "number") {
                                var arr = [];
                                arr.push(params.appsId)
                                return params;
                            }
                            fb.formSelects.value("appsId", params.appsId.split(","));
                        }
                        return params;
                    },
                    defines: [
                        {
                            head: "id",
                            prop: "id",
                        }, {
                            head: "分类名称",
                            prop: "groupName",
                        }, {
                            head: "分类描述",
                            prop: "groupDescription",
                            wordBreak:true
                        },
                        {
                            head: "创建时间",
                            prop: "gmt_create",
                            type: "datetime"
                        }
                    ],
                    handles: [
                        {
                            edit: function (data) {
                                return renders.box(data, false);
                            }
                        },
                        {
                            del: {
                                tip: "确定要删除吗？",
                                do: function (data) {
                                    req.delete(vars.reqUrl+data.id,function(data){
                                        console.log("======进入成功======");
                                        console.log("data:"+JSON.stringify(data));
                                        if(data.code == 0){
                                            layer.msg("删除成功",{icon:6});
                                            vars.table.flush();
                                        }
                                    })
                                }
                            }
                        },

                    ],
                });

            },
        },

    });



</script>